package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteNewsTitleNewsDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.StringUtils;
import com.wsj.util.tool.Tool;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.HomeSiteNews.AdminNewsTitleNewsForm;
import com.wsj.web.vo.homeSite.HomeSiteNewsTitleNews;

public class HomeSiteNewsTitleNewsDaoImpl extends HibernateDaoSupport implements IHomeSiteNewsTitleNewsDao 
{

	/******
	 * 添加标题新闻
	 */
	public Boolean addHomeSiteNewsTitleNewsDao(HomeSiteNewsTitleNews homeSiteNewsTitleNews) {
		this.getHibernateTemplate().save(homeSiteNewsTitleNews);
		return true;
	}

	public void deleteHomeSiteNewsTitleNewsDao(HomeSiteNewsTitleNews homeSiteNewsTitleNews)
	{
		this.getHibernateTemplate().delete(homeSiteNewsTitleNews);
		
	}

	public HomeSiteNewsTitleNews loadByIdDao(Integer id) 
	{
		Object obj=this.getHibernateTemplate().get(HomeSiteNewsTitleNews.class, id);
		if(obj!=null)
		{
			return  (HomeSiteNewsTitleNews)obj;
		}
		return null;
	}

	public PageResultSet loadHomeSiteNewsTitleNewsListDao(AdminNewsTitleNewsForm adminNewsTitleNewsForm) 
	{
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer(" from HomeSiteNewsTitleNews as h where 1=1 ");
		
		//页面
		Integer page=PageContent.pageValue;
		
		Integer pageSize=PageContent.pageSizeValue;
		
		if(adminNewsTitleNewsForm.getPage()!=null)
		{
			page=adminNewsTitleNewsForm.getPage();
		}
		
		if(adminNewsTitleNewsForm.getPageSize()!=null)
		{
			pageSize = adminNewsTitleNewsForm.getPageSize();
		}
		
		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
		// 参数
		List<Comparable> list = new ArrayList<Comparable>();
		
		if(adminNewsTitleNewsForm.getKeyWord()!=null  &&  !adminNewsTitleNewsForm.getKeyWord().equals(""))
		{
			stringBuffer.append(" and h.titleName like ?");
			list.add("%"+adminNewsTitleNewsForm.getKeyWord()+"%");
		}
		
		if(adminNewsTitleNewsForm.getTitleType()!=null&&adminNewsTitleNewsForm.getTitleType().intValue()!=-1)
		{
			stringBuffer.append(" and h.titleType = ?");
			list.add(adminNewsTitleNewsForm.getTitleType());
		}
		
		if(adminNewsTitleNewsForm.getParenId()!=null  && adminNewsTitleNewsForm.getParenId().intValue()!=-1)
		{
			stringBuffer.append(" and h.parenId = ?");
			list.add(adminNewsTitleNewsForm.getParenId());
		}
		
		/**
		 * 标题新闻出现的位置(首页还是资讯中心)
		 * index 总站首页 (默认)
		 * newsIndex 资讯中心
		 * */    
		if(adminNewsTitleNewsForm.getSiteType()!=null  && "index".equals(adminNewsTitleNewsForm.getSiteType()))
		{
			stringBuffer.append(" and h.siteType = ? ");
			list.add(adminNewsTitleNewsForm.getParenId());
		}
		
		
		stringBuffer.append(" order by h.addTime desc ");

		// 把集合转换成数组
		String[] strParams = Tool.toArray(list);

		pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);

		return pageResultSet;
		
	}

	/*****
	 * 更新 标题新闻
	 */
	public void updateDao(HomeSiteNewsTitleNews homeSiteNewsTitleNews) 
	{
		this.getHibernateTemplate().update(homeSiteNewsTitleNews);
	}

	/****
	 * 获取最新的标题
	 */
	public HomeSiteNewsTitleNews loadEndTitleNewsDao(String siteType) 
	{
		HomeSiteNewsTitleNews  ret = null;
		StringBuffer sqlBuff = new StringBuffer("from HomeSiteNewsTitleNews as h where h.titleType=0");
		if(StringUtils.isNotNull(siteType)){
			sqlBuff.append(" and h.siteType like '"+siteType+"' ");
		}
		sqlBuff.append("order by h.addTime  desc");
		Query query=this.getSession().createQuery(sqlBuff.toString());		
		query.setMaxResults(1);
		List li=query.list();
		if(li!=null && !li.isEmpty())
		{
			ret = (HomeSiteNewsTitleNews)li.get(0);
		}
		
		return ret;
		
	}

	/****
	 * 获取父类的列表
	 * id ,siteType 都不允许为空
	 */
	public List loadTitleNewsByParentId(Integer id,String siteType) 
	{
		return this.getHibernateTemplate().find(" from HomeSiteNewsTitleNews as h where h.parenId = ? and h.siteType like '"+siteType+"' ",id);		
	}
	

}
